package com.sita.tboard.usermgmt.helper;

import android.widget.ImageView;
import android.widget.TextView;

import com.sita.bike.persistence.TrendMember;
import com.sita.bike.rest.RestClient;
import com.sita.bike.rest.model.Account;
import com.sita.bike.rest.model.request.GetAccountRequest;
import com.sita.bike.rest.model.response.GetAccountResponse;
import com.sita.bike.support.GlobalContext;
import com.sita.bike.utils.PersistUtils;
import com.sita.tboard.ui.tools.L;
import com.sita.tboard.usermgmt.GetAccountInfoListener;
import com.sita.tboard.usermgmt.event.UserInfoFetchCompleteEvent;
import com.squareup.picasso.Picasso;

import de.greenrobot.event.EventBus;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by xiaodong on 16/3/28.
 */
public class UserNetworkHelper {
    private static class UserNetworkHelperHolder {
        private static UserNetworkHelper INSTANCE = new UserNetworkHelper();
    }

    private UserNetworkHelper() {
    }

    public static UserNetworkHelper getInstance() {
        return UserNetworkHelperHolder.INSTANCE;
    }

    public void getUserInfo(final String accountId) {
        GetAccountRequest req = new GetAccountRequest();
        req.accountId = accountId;
        req.showDetail = false;
        RestClient.getRestNormalService().accountGet(req, new Callback<GetAccountResponse>() {
            @Override
            public void success(GetAccountResponse resp, Response response) {
                Account account = resp.account;
                PersistUtils.saveTrendMember(account);
                EventBus.getDefault().post(new UserInfoFetchCompleteEvent(accountId));
            }

            @Override
            public void failure(RetrofitError error) {
                L.e("getUserInfo()", error);
            }
        });
    }

    public void getUserInfo(final String accountId, final ImageView avatarView,
                            final TextView nicknameView) {
        GetAccountRequest req = new GetAccountRequest();
        req.accountId = accountId;
        req.showDetail = false;
        RestClient.getRestNormalService().accountGet(req, new Callback<GetAccountResponse>() {
            @Override
            public void success(GetAccountResponse resp, Response response) {
                Account account = resp.account;
                PersistUtils.saveTrendMember(account);

                nicknameView.setText(account.mNickName);
                Picasso.with(GlobalContext.getGlobalContext()).load(account.mAvatar).fit()
                        .centerCrop().into(avatarView);
            }

            @Override
            public void failure(RetrofitError error) {
                L.e("getUserInfo()", error);
            }
        });
    }

    public void getUserInfo(final String accountId, final GetAccountInfoListener getAccountInfoListener) {
        TrendMember trendMember = PersistUtils.getTrendMember(accountId);
        if (trendMember != null){
            Account account = new Account();
            account.mAccountId = accountId;
            account.mNickName = trendMember.getNickname();
            account.mAvatar = trendMember.getAvatarUrl();
            getAccountInfoListener.onSuccess(account);
            return;
        }
        GetAccountRequest req = new GetAccountRequest();
        req.accountId = accountId;
        req.showDetail = false;
        RestClient.getRestNormalService().accountGet(req, new Callback<GetAccountResponse>() {
            @Override
            public void success(GetAccountResponse resp, Response response) {
                Account account = resp.account;
                PersistUtils.saveTrendMember(account);

                getAccountInfoListener.onSuccess(account);
            }

            @Override
            public void failure(RetrofitError error) {
                L.e("getUserInfo()", error);

                getAccountInfoListener.onFailure(error.getCause());
            }
        });
    }
}
